<template>
  <div class="add-form">
    <div class="nb-tittle">{{ title }}</div>
    <el-form ref="form" :model="data" label-width="160px" class="hx-form" hide-required-asterisk :rules="rules">
      <el-form-item label="项目名称：" prop="project_id">
        <v-select-s v-if="!editId" v-model="data.project_id" action="projectList" :query="{ status: 3 }" class="w180" @change="getBid" />
        <el-input v-if="editId" v-model="data.project.name" class="w180" />
      </el-form-item>
      <el-form-item label="发标单位：" prop="agent">
        <el-input v-model="data.agent" class="w180" />
      </el-form-item>
      <el-form-item label="中标金额：" prop="bid_sum">
        <el-input v-model="data.bid_sum" class="w180" :precision="2" readonly />
      </el-form-item>
      <el-form-item label="中标时间：" prop="winning_date">
        <el-date-picker
          v-model="data.winning_date"
          type="date"
          placeholder="选择日期"
          style="width: 180px "
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item v-if="editId" label="上传附件：">
        <Adjunct table_name="winning" :pk="editId" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('form')">提交</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import API from '@/api/winning/winning'
import VSelectS from '@/components/select-search'
import BID from '@/api/bid/bid'
import Adjunct from '@/components/Upload/Adjunct'
export default {
  inject: ['goUrl', 'isEdit', 'add', 'update', 'setStorage', 'clearStorage', 'getStorage'],
  components: { VSelectS, Adjunct },
  data() {
    return {
      data: {
        agent: '',
        bid_sum: '',
        project: {
          name: ''
        }
      },
      title: '',
      editId: '',
      value: '',
      rules: {
        project_id: { required: true, message: '请输入招标单位', trigger: 'change' },
        winning_date: { required: true, message: '请输入中标时间', trigger: 'change' },
        first_party: { required: true, message: '请输入发标单位', trigger: 'change' },
        bid_sum: { required: true, message: '请输入中标金额', trigger: 'change' }
      }
    }
  },
  created() {
    const edit = this.isEdit()
    if (edit) {
      this.title = '中标通知详情'
      this.editId = edit
      this.info()
    } else {
      this.title = '新增中标通知'
      if (this.getStorage('winning')) {
        this.data = this.getStorage('winning')
      }
    }
  },
  mounted() {
    window.addEventListener('beforeunload', e => this.beforeunloadHandler(e))
  },
  methods: {
    info() {
      API.detail(this.editId).then(res => {
        this.data = res.data
      })
    },
    beforeunloadHandler(e) {
      this.clearStorage('winning')
    },
    cancel() {
      if (!this.editId) {
        this.setStorage('winning', this.data)
      }
      this.goUrl('/winning')
    },
    getBid() {
      const query = {
        project_id: this.data.project_id
      }
      BID.bidList(query).then(res => {
        let bidName = ''
        let bidSum = ''
        if (res.data.data[0].name) {
          bidName = res.data.data[0].first_party
          bidSum = res.data.data[0].bid_sum
        }
        if (this.data.agent === '') {
          this.data.agent = bidName
        }
        if (this.data.bid_sum === '') {
          this.data.bid_sum = bidSum
        }
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.editId) {
            this.data.id = this.editId
            this.update(API, this.data, '更新成功')
          } else {
            delete this.data.project
            this.add(API, this.data, '新增成功', '/winning/edit/')
            this.clearStorage('customer')
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style>
.w180{
  width: 180px;
}
</style>
